package com.laker.autotest.api.service;

import com.laker.autotest.api.core.EasyOkHttpClient;
import com.laker.autotest.api.core.ServiceClientFactory;
import com.laker.autotest.api.core.auth.BearerAuthTokenProvider;
import okhttp3.Response;

import java.io.IOException;

/**
 * 一个 Service 对应一个 OkHttpClient 实例
 */
public class ReqresUserService {
    private static final String BASE_URL = "https://reqres.in/api";

    private final EasyOkHttpClient client;
    private final BearerAuthTokenProvider tokenProvider;

    public ReqresUserService() {
        this.tokenProvider = new BearerAuthTokenProvider("token");
        this.client = ServiceClientFactory.getClient(BASE_URL, this.tokenProvider);
    }

    /**
     * 获取所有用户
     */
    public Response getAllUsers() throws IOException {
        return client.get("/users");
    }

    /**
     * 根据 ID 获取用户
     */
    public Response getUserById(int id) throws IOException {
        return client.get("/users/" + id);
    }

    /**
     * 创建用户
     */
    public Response createUser(UserRequest user) throws IOException {
        return client.postJson("/users", user);
    }

    /**
     * 更新用户
     */
    public Response updateUser(int id, UserRequest user) throws IOException {
        return client.putJson("/users/" + id, user);
    }

    /**
     * 删除用户
     */
    public Response deleteUser(int id) throws IOException {
        return client.delete("/users/" + id);
    }
}